package com.goro;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.EJB;

import com.joro.Magazine;
import com.joro.ejb.MagazineFacadeLocal;
import javax.faces.component.UIComponent;
import javax.faces.component.UIForm;
import javax.faces.model.SelectItem;
public class MagazineBean {
	
	@EJB
	protected MagazineFacadeLocal magazineDao;
	private Magazine magazine;
	
	
	public MagazineBean() {
		magazine = new Magazine();
	}
	
	public Magazine getMagazine() {
		return magazine;
	}

	public void setMagazine(Magazine magazine) {
		this.magazine = magazine;
	}

	public Collection<Magazine> getMagazines(){
		return magazineDao.getMagazines();
	}
	
	public Collection<String> getMagazinesAsString() {
		List<String> res = new ArrayList<String>();
		Collection<Magazine> magazines = magazineDao.getMagazines();
		for (Magazine magazine : magazines) {
			res.add(magazine.getName());
		}
		return res;
	}
	
	public Collection<SelectItem> getMagazineItems() {
		List<SelectItem> res = new ArrayList<SelectItem>();
		Collection<Magazine> magazines = magazineDao.getMagazines();
		for (Magazine magazine : magazines) {
			SelectItem item = new SelectItem();
			item.setLabel(magazine.getName());
			item.setValue(magazine.getId());
			res.add(item);
		}
		return res;
	}
	
	
	public String deleteMagazine() {
		magazineDao.deleteMagazine(magazine);		
		return null;
	}
}
